import * as types from "../types/homeTypes";
import NetWorkUtil from "../utils/NetWorkUtil";
import ApiConstants from "../common/ApiConstants";

export function getBanners() {
  return dispatch => {
    dispatch({ type: types.HOME_BANNER_INITING });
    NetWorkUtil.getWithoutLogin(ApiConstants.HOME_BANNER, {})
      .then(res => {
        console.log(res);
        dispatch({ type: types.HOME_BANNER_DONE, banners: res.list });
      })
      .catch(error => {
        dispatch({ type: types.HOME_BANNER_ERROR, error });
      });
  };
}


export function getHotTagInfoList(brandId) {
  return dispatch => {
    dispatch({ type: types.HOTLIST_INITING });
    NetWorkUtil.getWithoutLogin(ApiConstants.HOME_HOTBRANDINFOLIST, { brand_id: brandId })
      .then(res => {
        console.log(res);
        dispatch({ type: types.HOTLIST_DONE, hotInfoList: res.list });
      })
      .catch(error => {
        dispatch({ type: types.HOTLIST_ERROR, error });
      });
  };
}

export function getHotTagList() {
  return dispatch => {
    dispatch({ type: types.HOTTAGS_INITING });
    NetWorkUtil.getWithoutLogin(ApiConstants.HOME_HOTBRANDLIST, {})
      .then(res => {
        let brandId = 0;
        if (res.count > 0) {
          brandId = res.list[0].brand_id;
          dispatch(getHotTagInfoList(brandId));
        }
        dispatch({ type: types.HOTTAGS_DONE, brandId, hotTags: res.list });
      })
      .catch(error => {
        dispatch({ type: types.HOTTAGS_ERROR, error });
      });
  };
}
export function onTagClick(hotTag) {
  const { brand_id } = hotTag;
  return dispatch => {
    dispatch({ type: types.HOTLIST_CHANGETAG, brandId: brand_id });
    dispatch(getHotTagInfoList(brand_id));
  };
}
